package com.wn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.ManagerDao;
import com.wn.entity.Manager;
import com.wn.entity.ManagerDTO;
import com.wn.entity.ResponseEntity;
import com.wn.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ManagerServiceImpl implements ManagerService {
    @Autowired
    private ManagerDao managerDao;

    @Override
    @Transactional
    public ResponseEntity addOne(Manager manager) {
        int i = managerDao.addOne(manager);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.fail;
        }
    }

    @Override
    @Transactional
    public ResponseEntity deleteOne(int id) {
        int i = managerDao.deleteOne(id);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.fail;
        }
    }

    @Override
    @Transactional
    public ResponseEntity update(Manager manager) {
        int i = managerDao.update(manager);
        if (i > 0) {
            return ResponseEntity.SUCCESS;
        } else {
            return ResponseEntity.fail;
        }
    }

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<ManagerDTO> all = managerDao.findAll();
        PageInfo<ManagerDTO> pageInfo = PageInfo.of(all);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }

    @Override
    public ResponseEntity findAll() {

        List<ManagerDTO> managers = managerDao.findAll();
        return new ResponseEntity(200, "成功", managers);
    }

    @Override
    public ResponseEntity findByCondition(String account, String status,int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Manager> mangers = managerDao.findByAccountAndStatus(account,status);
        PageInfo<Manager> pageInfo = PageInfo.of(mangers);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }

    @Override
    public Manager findByUsername(String username) {
        return managerDao.findByUsername(username);
    }

    @Override
    public Manager login(String username, String password) {
        return managerDao.login(username,password);
    }
}
